#!/bin/bash
wget https://ecs-instance-driver.obs.cn-north-1.myhuaweicloud.com/datadisk/LinuxVMDataDiskAutoInitialize.sh
chmod +x LinuxVMDataDiskAutoInitialize.sh
yum install expect -y
/usr/bin/expect<<EOF
spawn ./LinuxVMDataDiskAutoInitialize.sh
expect "/dev/vdb and q to quit"
send "/dev/vdb\r"
expect "/mnt/data"
send "/datadisk\r"
expect eof
exit
EOF
rm -rf LinuxVMDataDiskAutoInitialize.sh
wget -i -c -P /datadisk http://dev.mysql.com/get/mysql57-community-release-el7-10.noarch.rpm
cd /datadisk
yum -y install mysql57-community-release-el7-10.noarch.rpm
yum -y install mysql-community-server --nogpgcheck
systemctl start mysqld
systemctl enable mysqld
systemctl status mysqld.service
mypwd=`grep "temporary password" /var/log/mysqld.log|awk -F' ' "{print $NF}"|awk '{print $NF}'`
/usr/bin/expect <<-EOF
spawn mysql_secure_installation
expect "Enter password for user root:"
send "$mypwd\r"
expect "New password:"
send "$1\r"
expect "Re-enter new password:"
send "$1\r"
expect " any other key for No) :"
send "N\r"
expect " any other key for No) :"
send "Y\r"
expect " any other key for No) :"
send "Y\r"
expect " any other key for No) :"
send "Y\r"
expect " any other key for No) :"
send "Y\r"
expect eof
exit
EOF
/usr/bin/expect <<-EOF
spawn mysql -u root -p
expect "Enter password: "
send "$1\r"
expect "mysql>"
send "use mysql;\r"
expect "mysql>"
send "select host,user from user;\r"
expect "mysql>"
send "update user set host='%' where user='root' LIMIT 1;\r"
expect "mysql>"
send "flush privileges;\r"
expect "mysql>"
send  "quit\r"
expect eof
exit
EOF
systemctl start mysqld
systemctl enable mysqld
systemctl stop firewalld.service
systemctl status firewalld



